const app = getApp();
const {
  http
} = require("../../../utils/apiutils")
const
  util = require("../../../utils/util")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBar: 0,
    CustomBar: 0,
    userInfo: {},
    sum: {
      yjs: 0,
      yzp: 0,
    },
    exist: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let CustomBar = app.globalData.CustomBar;
    let statusBar = app.globalData.statusBar
    this.setData({
      CustomBar: CustomBar - statusBar,
      statusBar: statusBar,
      userInfo: app.globalData.userInfo,
    })
    console.log("-------------", this.data.statusBar);
  },
  onclickCell: function (e) {
    // let isTrue = getApp().globalData.check();
    // if(!isTrue) return 
    let index = e.currentTarget.dataset.index;
    // console.log("-----",this.data.exist,index)
    if (this.data.exist == "0" && index != 1) {
      wx.showModal({
        title: "提示",
        content: "您尚未添加公司信息暂无法使用",
        cancelText: '稍后认证',
        confirmText: '去认证',
        success: function (e) {
          if (e.confirm) {
            wx.navigateTo({
              // url: '/pages/me/pages/companyInfo/companyInfo',
              url: '/pages/me/pages/savecompany/index',
            })
          }
          if (e.cancel) {
            http.laterCertification({}, res => {
              if (res.status == 200) {
                http.userInfo({}, res => {
                  if (res.code == 0) {
                    getApp().globalData.userInfo = res.data;
                  } else {
                    wx.showToast({
                      title: res.message,
                      icon: "none",
                    })
                  }
                }, err => { })
                wx.reLaunch({
                  url: '/pages/tabBar/home/index',
                })
              }

            }, err => { })
          }
        },
      })
      return
    }
    if (index == 2) {
      wx.navigateTo({
        url: '/pages/me/pages/company/company',
      })
    } else if (index == 3) {
      wx.navigateTo({
        url: '/pages/me/pages/address/address',
      })
    } else if (index == 1) {
      if (this.data.exist == 1) {
        wx.navigateTo({
          url: '/pages/me/pages/companyInfo/companyInfo',
        })
        return
      }
      wx.navigateTo({
        // url: '/pages/me/pages/companyInfo/companyInfo',
        url: '/pages/me/pages/savecompany/index',
      })
    } else if (index == 5) {
      wx.navigateTo({
        url: '/pages/me/pages/security/security',
      })
    } else if (index == 11) {
      wx.navigateTo({
        url: '/pages/me/pages/nickname/nickname',
      })
    } else if (index == 21) {
      wx.navigateTo({
        url: '/pages/me/pages/action/action?type=1',
      })
    } else if (index == 22) {
      wx.navigateTo({
        url: '/pages/me/pages/action/action?type=2',
      })
    } else if (index == 24) {
      wx.navigateTo({
        url: '/pages/me/pages/action1/index',
      })
    } else if (index == 23) {
      wx.navigateTo({
        url: '/pages/me/pages/action/action?type=3',
      })
    } else if (index == 0) {
      wx.navigateTo({
        url: '/pages/me/pages/plan/plan',
      })
    } else if (index == 'zfb') {
      wx.navigateTo({
        url: '/pages/me/pages/zfb/zfb',
      })
    } else if (index == 'wxpay') {
      wx.navigateTo({
        url: '/pages/me/pages/wxpay/wxpay',
      })
    } else if (index == '9') {
      wx.navigateTo({
        url: '/pages/me/pages/sharecompany/index',
      })
    } else if (index == 'wdsy') {
      wx.navigateTo({
        url: '/pages/me/pages/profit/profit',
      })
    } else if (index == 'sck') {
      wx.navigateTo({
        url: '/pages/me/pages/sck/sck',
      })
    }
    // console.log("-----------", e);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      exist: getApp().globalData.userInfo.exist,
    })
    console.log(getApp().globalData.userInfo.exist)
    if (this.data.exist != "0") {
      this.getsalarySum()
    }
    this.setData({
      userInfo: app.globalData.userInfo,
    })
  },
  getsalarySum: function () {

    var that = this;
    http.getsalarySum({},
      function (res) {
        that.setData({
          sum: res
        })
      },
      function (err) {
        wx.hideLoading()
      })




  },

  /**上传图片 */
  onclickSelect: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        let imgSrc = res.tempFilePaths[0];
        that.uploadImage(imgSrc);
        console.log("------", res);
      },
    })
  },
  /**上传图片 */
  uploadImage: function (scr) {
    var data = {
      url: scr,
      name: "avatar",
      formData: {},
    }
    var that = this;
    wx.showLoading({
      title: '上传种...',
    })
    http.upavatar(data,
      function (res) {
        wx.hideLoading()
        console.log("------上传头像---", res);
        if (res.code == 0) {
          wx.showToast({
            title: "上传成功",
            icon: "none",
          })
          that.getuser();
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })
  },
  //获取用户信息
  getuser: function () {

    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.userInfo({},
      function (res) {
        wx.hideLoading()
        console.log("------用户信息--", res);
        if (res.code == 0) {
          getApp().globalData.userInfo = res.data;
          that.setData({
            userInfo: res.data
          })

        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var shareObj = {
      title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
      path: '/pages/index/index', // 
      imageUrl: '',
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') { }
      }
    }
    console.log("------------", shareObj);
    // 返回shareObj
    return shareObj;
  }
})